package com.zgkxzx.mvvmbase.viewmodel;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.zgkxzx.mvvmbase.api.RestApi;
import com.zgkxzx.mvvmbase.bean.picture.PictureBean;
import com.zgkxzx.mvvmbase.bean.picture.PictureResultBean;

import java.util.List;

import rx.Subscriber;

/**
 * 创建者 zgkxzx
 * 日期   1/31/18
 * 描述
 */

public class MainViewModel extends BaseViewModel {
    private static final String TAG = MainViewModel.class.getSimpleName();

    private OnDataListener onDataListener;

    public MainViewModel(Context context, OnDataListener onDataListener) {
        super(context);
        this.onDataListener = onDataListener;
        loadData();
    }

    //Text事件处理
    public void onTextClick(View view) {
        Log.d(TAG, "onTextClick ");

        loadData();
    }


    public void loadData() {
        RestApi.getPictureList(new Subscriber<PictureResultBean>() {

            @Override
            public void onCompleted() {
                Log.d(TAG, "onCompleted ");
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError " + e);
            }

            @Override
            public void onNext(PictureResultBean pictureResultBean) {
                Log.d(TAG, "onNext pictureResultBean " + pictureResultBean);
                if (onDataListener != null && pictureResultBean != null)
                    onDataListener.onData(pictureResultBean.getResults());
            }
        }, 1);
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }


    public interface OnDataListener {
        void onData(List<PictureBean> results);
    }


}
